#include "Pong.h"

void PongGame::GameInit()
{
	paddleHeight = 125;
	paddleWidth = 10;

	leftLoc.XValue = 80;
	leftLoc.YValue = SCREENHEIGHT/2;
	leftVel = 0;
	
	rightLoc.XValue = SCREENWIDTH - 80;
	rightLoc.YValue = SCREENHEIGHT/2;
	rightVel = 0;

	puckLoc.XValue = SCREENWIDTH / 2;
	puckLoc.YValue = SCREENHEIGHT / 2;
	puckRadius = 10;

	leftPaddle.init(leftLoc,paddleHeight,paddleWidth);

	rightPaddle.init(rightLoc,paddleHeight,paddleWidth);

	puck.init(puckLoc, puckRadius);

	startButton.Initialise(SCREENHEIGHT-100,SCREENWIDTH-370,SCREENHEIGHT-50,SCREENWIDTH-220, "START");

	endButton.Initialise(SCREENHEIGHT-100,SCREENWIDTH-200,SCREENHEIGHT-50,SCREENWIDTH-50, "END");

	lScore = 0;
	rScore = 0;

	lWins = 0;
	rWins = 0;

	theState = STARTUP;

	setGameActive(true);
}

void PongGame::GameMain()
{


	if (theState == STARTUP)
	{
		TextWriter(Vector2D(30,50),"PLAYER WINS: ",MyDrawEngine::DARKGREEN);
		IntWriter(Vector2D(180,50), lWins ,MyDrawEngine::DARKGREEN);

		TextWriter(Vector2D(30,80),"COMPUTER WINS: ",MyDrawEngine::DARKGREEN);
		IntWriter(Vector2D(220,80), rWins ,MyDrawEngine::DARKGREEN);

		startButton.Update(gTheMouse);
		startButton.Draw();

		endButton.Update(gTheMouse);
		endButton.Draw();

		gTheMouse.Update(SCREENWIDTH, SCREENHEIGHT);
		gTheMouse.Draw();

		if (startButton.getActive())
		{
			gameStart();

			theState = INGAME;
		}

		if (endButton.getActive())
		{
			theState = WRAPUP;
		}
	}
	else if (theState == INGAME)
	{
		
		theGameLoop();

		if (lScore == maxScore || rScore == maxScore)
		{
			if(lScore > rScore)
			{
				lWins++;
			}
			else
			{
				rWins++;
			}

			lScore = 0;
			rScore = 0;

			gameStart();

			theState = STARTUP;
		}
	}
	else if (theState == WRAPUP)
	{


		GameShutdown();
	}
	else 
	{
		GameShutdown();
	}


}

void PongGame::GameShutdown()
{
	setGameActive(false);

	theResult.score = (lWins-rWins)*100;
}

void PongGame::theGameLoop()
{
	TextWriter(Vector2D(230,50),"PLAYER SCORE: ",MyDrawEngine::DARKGREEN);
	IntWriter(Vector2D(400,50), lScore ,MyDrawEngine::DARKGREEN);

	TextWriter(Vector2D(530,50),"COMPUTER SCORE: ",MyDrawEngine::DARKGREEN);
	IntWriter(Vector2D(740,50) , rScore ,MyDrawEngine::DARKGREEN);

	leftControl();
	rightControl();
	puckCheck();

	scoreCheck();
}

void PongGame::leftControl()
{
	if (leftPaddle.getPosition().YValue-(paddleHeight/2) < 0 ||
		leftPaddle.getPosition().YValue+(paddleHeight/2) > SCREENHEIGHT)
	{
		if (leftPaddle.getPosition().YValue-(paddleHeight/2) < 0)
		{
			leftLoc.YValue = 0 + paddleHeight/2;
		}
		else if (leftPaddle.getPosition().YValue+(paddleHeight/2) > SCREENHEIGHT)
		{
			leftLoc.YValue = SCREENHEIGHT - paddleHeight/2;
		}

		leftVel = 0;
		leftPaddle.setPosition(leftLoc);
	}
	else if (KEYPRESSED('W')||KEYPRESSED('A'))
	{
		leftVel -= 0.25;
	}
	else if (KEYPRESSED('S')||KEYPRESSED('D'))
	{
		leftVel += 0.25;
	}
	else if (leftVel > 0)
	{
		leftVel -= 0.25;		
	}
	else if (leftVel < 0)
	{
		leftVel += 0.25;
	}
	else 
	{
		leftVel = 0;
	}

	leftPaddle.Update(Vector2D(0,leftVel));
	leftPaddle.Draw();
}

void PongGame::rightControl()
{
		if (rightPaddle.getPosition().YValue-(paddleHeight/2) < 0 ||
			rightPaddle.getPosition().YValue+(paddleHeight/2) > SCREENHEIGHT)
		{
			if (rightPaddle.getPosition().YValue-(paddleHeight/2) < 0)
			{
				rightLoc.YValue = 0 + paddleHeight/2;
			}
			else if (rightPaddle.getPosition().YValue+(paddleHeight/2) > SCREENHEIGHT)
			{
				rightLoc.YValue = SCREENHEIGHT - paddleHeight/2;
			}

			rightVel = 0;
			rightPaddle.setPosition(rightLoc);
		}
		else if (puck.getPosition().YValue < rightPaddle.getPosition().YValue)
		{
			rightVel -= 0.25;
		}
		else if (puck.getPosition().YValue > rightPaddle.getPosition().YValue)
		{
			rightVel += 0.25;
		}

	rightPaddle.Update(Vector2D(0,rightVel));
	rightPaddle.Draw();
}

void PongGame::puckCheck()
{

	if (puck.getPosition().YValue - puckRadius < 0 ||
		puck.getPosition().YValue + puckRadius > SCREENHEIGHT)
	{
		if (puck.getPosition().YValue - puckRadius < 0)
		{
			puckLoc.YValue = 0 + puckRadius;
		}
		else if (puck.getPosition().YValue + puckRadius > SCREENHEIGHT)
		{
			puckLoc.YValue = SCREENHEIGHT - puckRadius;
		}

		puckLoc.XValue = puck.getPosition().XValue;

		puckVelocity.YValue = -puckVelocity.YValue;
		puck.setPosition(puckLoc);
	}

	if (puck.CollidesWith(leftPaddle)|| puck.CollidesWith(rightPaddle))
	{
		if (puck.CollidesWith(leftPaddle))
		{
			puckLoc.XValue = leftPaddle.getPosition().XValue + leftPaddle.getWidth() + puckRadius;

			if (puck.getPosition().YValue > leftPaddle.getPosition().YValue)
			{
				puckVelocity.YValue += 0.25;
			}
			else
			{
				puckVelocity.YValue -= 0.25;
			}

		}
		else if (puck.CollidesWith(rightPaddle))
		{
			puckLoc.XValue = rightPaddle.getPosition().XValue - rightPaddle.getWidth() - puckRadius;

			if (puck.getPosition().YValue > rightPaddle.getPosition().YValue)
			{
				puckVelocity.YValue += 0.25;
			}
			else
			{
				puckVelocity.YValue -= 0.25;
			}
		}

		puckLoc.YValue = puck.getPosition().YValue;

		if (puckVelocity.XValue > 0)
		{
			puckVelocity.XValue = -(puckVelocity.XValue+1);
		}
		else 
		{
			puckVelocity.XValue = -(puckVelocity.XValue-1);
		}

		if (puckVelocity.XValue > maxPuckVel)
		{
			puckVelocity.XValue = maxPuckVel;
		}
		else if (puckVelocity.XValue < -maxPuckVel)
		{
			puckVelocity.XValue = -maxPuckVel;
		}
					
		
		puck.setPosition(puckLoc);
	}

	puck.Update(puckVelocity);
	puck.Draw();
}

void PongGame::gameStart()
{
	leftLoc.XValue = 80;
	leftLoc.YValue = SCREENHEIGHT/2;
	leftVel = 0;
	
	rightLoc.XValue = SCREENWIDTH - 80;
	rightLoc.YValue = SCREENHEIGHT/2;
	rightVel = 0;

	puckLoc.XValue = SCREENWIDTH / 2;
	puckLoc.YValue = SCREENHEIGHT / 2;
	puck.setPosition(puckLoc);

	do
	{
		puckVelocity.XValue = float(rand()%20-10);
		puckVelocity.YValue = float(rand()%20-10);
	} while(puckVelocity.XValue == 0 || puckVelocity.YValue == 0);
}

void PongGame::scoreCheck()
{
	if (countdown == true)
	{
		if (reClock > 0 && theClock.ticker())
		{
			reClock--;
		}
		else if (reClock == 0)
		{
			countdown = false;

			gameStart();
		}
	}
	else if (puck.getPosition().XValue < 0 && countdown == false)
	{
		rScore++;
		reClock = 5;
		countdown = true;
	}
	else if (puck.getPosition().XValue > SCREENWIDTH && countdown == false)
	{
		lScore++;
		reClock = 5;
		countdown = true;
	}
	else 
	{
		countdown = false;
	}



}

GameResult PongGame::GetResult()
{
	return theResult;
}

Game* GetPongGame()
{
	return new PongGame;
}